class Solution {
    //解法1： 分割+拼接
    public String reverseWords(String s) {
        s = s.trim();
        String[] str = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = str.length-1; i >= 0; i--) {
            if (!str[i].equals(""))
                sb.append(str[i]+" ");
        }
        return sb.toString().trim();
    }
    //解法2：双指针
    public String reverseWords2(String s) {
        s = s.trim();
        int i = s.length() - 1;
        int j = i;
        StringBuilder sb = new StringBuilder();
        while (i >= 0) {
            while (i >= 0 && s.charAt(j) != ' ') i--;
            sb.append(s.substring(i+1,j+1) + " ");
            while (i>=0 && s.charAt(i) == ' ') i--;
            j = i;
        }
        return sb.toString().trim();
    }
}